package com.atguigu.cms.controller;


import com.atguigu.cms.entity.CrmBanner;
import com.atguigu.cms.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author dujiajia
 * @since 2020-11-05
 */
@RestController
@RequestMapping("/educms/banner")
@CrossOrigin
@Api(tags = "banner管理")
public class CrmBannerController {

    @Autowired
    private CrmBannerService crmBannerService;

    @ApiOperation("分页查询banner")
    @GetMapping("getPageBanner")
    public R getPageBanner(@RequestParam long pageNum,@RequestParam long pageSize) {
        Page<CrmBanner> page = new Page<>(pageNum,pageSize);
        crmBannerService.page(page,null);
        List<CrmBanner> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total",total).data("records",records);
    }

    @ApiOperation("新增banner")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean save = crmBannerService.save(crmBanner);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("更新banner")
    @PutMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        boolean update = crmBannerService.updateById(crmBanner);
        if (update){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("删除banner")
    @DeleteMapping("deleteBanner/{id}")
    public R deleteBanner(@PathVariable String id) {
        crmBannerService.removeById(id);
        return R.ok();
    }
}

